import requests
from bs4 import BeautifulSoup

home_url = 'https://ibaotu.com/shipin/7-0-0-0-0-1.html?so=0&mrltype=1&clear=92903&duration=0'
headers = {
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 QuarkPC/4.3.2.475",
    "sec-ch-ua-platform": "macOS",
    "referer": "https://ibaotu.com/",
    "origin": "https://ibaotu.com",
    "accept-language": "zh-CN,zh;q=0.9",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept": "application/json, text/javascript, */*; q=0.01",
}


def get_video(url):
    """
    获取视频：包括视频链接信息和标签信息
    this method is used to get the url and the title of the resource what in the gaven url website
    :param url:the gaven website's url
    :return:the resources in the gaven url，
    """
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        video_urls = __get_video_url(soup)
        video_names = __get_video_title(soup)
        video_tags = __get_video_tags(soup)
        if not video_urls or not video_names or not video_tags:
            raise Exception("资源信息获取失败")
        return list(zip(video_names, video_urls)), video_tags
    else:
        raise Exception("网络连接失败")


def __video_url_protocol_checker(video):
    """
    check the protocol of the url what the gaven resource
    :param resource:the gaven resource
    :return:the resource what after checked
    """
    if not video['src'].startswith('https:'):
        video['src'] = 'https:' + video['src']
    if '.mp4_bs5' in video['src']:
        video['src'] = video['src'].replace('.mp4_bs5', '')

    return video['src']


def __get_video_url(soup):
    """
    filter the resource that non src attr
    获取视频链接
    :param soup:
    :return:
    """
    videos_url = soup.find_all('video', recursive=True)
    filtered_videos_url = filter(lambda video_url: 'src' in video_url.attrs, videos_url)
    processed_videos_url = map(__video_url_protocol_checker, filtered_videos_url)
    return list(processed_videos_url)


def __get_video_title(soup):
    """
    获取视频标题
    :param soup:
    :return:
    """
    videos_title = soup.find_all('div', class_='pr-container', recursive=True)
    processed_videos_titles = map(lambda video: video['pr-data-title'], videos_title)
    return list(processed_videos_titles)


def __get_video_tags(soup):
    """
    获取视频标签
    :param soup:
    :return:
    """
    videos_tags = soup.find_all('a', class_='nav-tags-items-a active')
    processed_videos_tags = map(lambda video_tag: video_tag.get_text(), videos_tags)
    return list(processed_videos_tags)


if __name__ == "__main__":
    try:
        result = get_video(home_url)
        print(result)
    except Exception as e:
        print(e)
